package com.cloudlead.utils.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dean on 2015/4/30.
 */
public class FileUploadClient implements FileUpload {

    private String fileAddressFormat = "http://%s.oss-cn-hangzhou.aliyuncs.com/%s";

    private final String apiKey;

    private final String secretKey;

    private final String bucketName;

    private final OSSClient client;

    public FileUploadClient(String apiKey, String secretKey, String bucketName) {
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.bucketName = bucketName;
        client = new OSSClient(apiKey, secretKey);
    }

    @Override
    public void uploadFile(String path, String fileName, File file) throws FileNotFoundException {
        if (null == fileName || "".equals(fileName)) {
            fileName = file.getName();
        }
        uploadFile(path, fileName, file.length(), new FileInputStream(file));
    }

    @Override
    public String uploadFile(String path, String fileName, long contentLength, InputStream fileData) {
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(contentLength);

        String key = getKey(path, fileName);
        client.putObject(bucketName, key, fileData, objectMeta);
        return key;
    }

    private String getKey(String path, String fileName) {
        String key = "";
        if (null != path && !"".equals(path)) {
            while (path.startsWith("/") || path.startsWith("\\")) {
                path = path.substring(1);
            }
            if (!path.endsWith("/")) {
                path += "/";
            }
            key += path;
        }

        key += fileName;
        return key;
    }

    @Override
    public String getAddress(String key) {
        return String.format(fileAddressFormat, bucketName, key);
    }

    @Override
    public void downloadFile(String key, File fileDownloadPath) {
        client.getObject(new GetObjectRequest(bucketName, key), fileDownloadPath);
    }

    @Override
    public FileContent downloadFile(String key) {
        OSSObject ossObject = client.getObject(bucketName, key);

        FileContent fileContent = new FileContent();
        fileContent.setContentType(ossObject.getObjectMetadata().getContentType());
        fileContent.setContentLength(ossObject.getObjectMetadata().getContentLength());
        fileContent.setContent(ossObject.getObjectContent());
        return fileContent;
    }

    @Override
    public List<String> listFile(String path) {
        ObjectListing objectListing = client.listObjects(bucketName, path);
        List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
        List<String> files = new ArrayList<>();
        for (OSSObjectSummary summary : objectSummaries) {
            files.add(summary.getKey());
        }
        return files;
    }
}
